import { PetNewBaseView } from "../PetNewBaseView";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { PetEventName } from "../PetEventName";
import { SMoney } from "../../../../../Message/Game/SMoney";
import { Cache } from "../../../cache/Cache";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { Language } from "../../../../../extend/language/Language";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { PetTileItem } from "../PetTileItem";
import { GImageBitmap } from "../../../../../com/mui/controls/GImageBitmap";
import { TilePanel } from "../../equipment/TilePanel";
import { ScrollPolicy } from "../../../../../fl/controls/ScrollPolicy";
import { PetSkillBookRenderer } from "./PetSkillBookRenderer";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { BaseItem } from "../../common/BaseItem";
import { PetSkillYiRenderer } from "./PetSkillYiRenderer";
import { PetStudySkillListPanel } from "../skillList/PetStudySkillListPanel";
import { PublicMessageConfig } from "../../../resource/PublicMessageConfig";
import { SPetInfo } from "../../../../../Message/Game/SPetInfo";
import { PetColor } from "../info/PetColor";
import { ItemData } from "../../../resource/info/ItemData";
import { ItemsUtil } from "../../shortcuts/item/ItemsUtil";
import { QuickBuyWindow } from "../../common/QuickBuyWindow";
import { ECategory } from "../../../../../Message/Public/ECategory";
import { EProp } from "../../../../../Message/Public/EProp";
import { MsgManager } from "../../../manager/MsgManager";
import { SkillConfig } from "../../../resource/SkillConfig";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { GameController } from "../../../mvc/GameController";
import { GuideTipsManager } from "../../../manager/GuideTipsManager";
import { TSkill } from "../../../../../Message/Db/Tables/TSkill";
import { PetSkillListPanel } from "./PetSkillListPanel";
import { _petInfo } from "../../../../../this/_petInfo";
type int = number;
//class PetSkillPanel
    
    export  class PetSkillPanel extends PetNewBaseView
    {
        constructor()
        {
            super();
            this._skillItemArray = [];
            this._bookDP = new DataProvider();
            this._yiDP = new DataProvider();
            Dispatcher.addEventListener(PetEventName.PetSkillBookSelected, this.onPetSkillBookSelectedHandler);
            return;
        }

        protected /* override */ onMoneyUpdateHandler(arg1: SMoney=null): void
        {
            var loc1=Cache.instance.role.money;
            if (loc1.coinBind + loc1.coin < this._studyCost) 
            {
                this._costTxt.htmlText = HTMLUtil.addColor(this._studyCost + Language.getString(54145), GlobalStyle.color7);
            }
            else 
            {
                this._costTxt.htmlText = HTMLUtil.addColor(this._studyCost + Language.getString(54145), GlobalStyle.color1);
            }
            return;
        }

        public /* override */ update(arg1: Object, ... rest): void
        {
            super.update(arg1, rest);
            this.initListData();
            return;
        }

        protected /* override */ init(): void
        {
            ObjCreate.createBg(0, 0, 582, 325, this);
            ObjCreate.createBg(0, 326, 582, 74, this);
            ObjCreate.createBg(30, 27, 480, 1, this, ImagesConst.SeptalLine);
            ObjCreate.createFlowerBg(5, 202, 572, 120, this);
            this._tileItem = new PetTileItem();
            this._tileItem.x = 60;
            this._tileItem.y = 10;
            this.addChild(this._tileItem);
            this._nameTxt = ObjCreate.createTextField("", 125, 10, 125, 20, this);
            this._nameTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            ObjCreate.createBitmap(ImagesConst.Fire, 248, 0, this);
            ObjCreate.createTextField(Language.getString(54562), 270, 10, 300, 20, this, GlobalStyle.textFormat5);
            this._leftSprite = new Object /* flash.display.Sprite */();
            this._leftSprite.x = 5;
            this._leftSprite.y = 35;
            this.addChild(this._leftSprite);
            this._leftSprite.addChild(new GImageBitmap("PetOwnBookLabel.png", 0, 0));
            ObjCreate.createBg(0, 20, 205, 145, this._leftSprite);
            this._helpBtn = ObjCreate.createGButton(Language.getString(54142), 60, 0, 50, 24, this._leftSprite);
            this._helpBtn.name = "help";
            this._helpBtn.visible = false;
            this._helpBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            this._bookList = new TilePanel();
            this._bookList.horizontalGap = 0;
            this._bookList.verticalGap = 5;
            this._bookList.columnWidth = 48;
            this._bookList.rowHeight = 65;
            this._bookList.setSize(210, 130);
            this._bookList.move(10, 25);
            this._bookList.scrollPolicy = ScrollPolicy.AUTO;
            this._bookList.setStyle("cellRenderer", PetSkillBookRenderer);
            this._leftSprite.addChild(this._bookList);
            this._centerSprite = new Object /* flash.display.Sprite */();
            this._centerSprite.x = 225;
            this._centerSprite.y = 35;
            this.addChild(this._centerSprite);
            ObjCreate.createTextField(Language.getString(54143), 5, 0, 220, 50, this._centerSprite, new GTextFormat(FontUtil.songtiName, 14, 15788607));
            ObjCreate.createBg(45, 33, 38, 38, this._centerSprite, ImagesConst.PackItemBg);
            this._studyItem = new BaseItem();
            this._studyItem.setSize(32, 32);
            this._studyItem.move(47, 35);
            this._studyItem.addEventListener(flash.events.MouseEvent.CLICK, this.onStudyItemClickHandler);
            this._centerSprite.addChild(this._studyItem);
            this._costTip = ObjCreate.createTextField(Language.getString(54144), 0, 85, 150, 20, this._centerSprite);
            this._costTxt = ObjCreate.createTextField(this._studyCost + Language.getString(54145), 85, 85, 150, 20, this._centerSprite);
            this._studyBtn = ObjCreate.createGButton(Language.getString(54146), 30, 110, 70, 26, this._centerSprite, "ButtonSpecial");
            this._studyBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onStudyBtnClickHandler);
            this._rightSprite = new Object /* flash.display.Sprite */();
            this._rightSprite.x = 361;
            this._rightSprite.y = 35;
            this.addChild(this._rightSprite);
            this._rightSprite.addChild(new GImageBitmap("PetYiStoneLabel.png", 0, 0));
            this._illuBtn = ObjCreate.createGButton(Language.getString(54148), 125, -5, 70, 24, this._rightSprite);
            this._illuBtn.name = "illu";
            this._illuBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            ObjCreate.createBg(0, 20, 205, 145, this._rightSprite);
            this._yiList = new TilePanel();
            this._yiList.horizontalGap = 0;
            this._yiList.verticalGap = 5;
            this._yiList.columnWidth = 48;
            this._yiList.rowHeight = 65;
            this._yiList.setSize(210, 135);
            this._yiList.move(10, 25);
            this._yiList.scrollPolicy = ScrollPolicy.AUTO;
            this._yiList.setStyle("cellRenderer", PetSkillYiRenderer);
            this._rightSprite.addChild(this._yiList);
            this._skillSprite = new Object /* flash.display.Sprite */();
            this._skillSprite.x = 42;
            this._skillSprite.y = 212;
            this.addChild(this._skillSprite);
            this._skillListPanel = new PetStudySkillListPanel();
            this._skillListPanel.y = 25;
            this._skillListPanel.addEventListener(flash.events.Event.CHANGE, this.onSelectChangeHandler);
            this._skillSprite.addChild(this._skillListPanel);
            this._forgetBtn = ObjCreate.createGButton(Language.getString(54149), 259, 177, 60, 24, this);
            this._forgetBtn.name = "forget";
            this._forgetBtn.visible = false;
            this._forgetBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            super.init();
            updateTip(PublicMessageConfig.instance.getPetForgeTextByName("studySkillDesc"), 10, 330, 100);
            _tipTxt.addEventListener(flash.events.TextEvent.LINK, this.onLinkClickHandler);
            return;
        }

        public /* override */ updateByPetInfo(arg1: SPetInfo): void
        {
            this._petInfo = arg1;
            var loc1="";
            if (arg1) 
            {
                this.updateSkillSprite(arg1);
                this._studyItem.itemData = null;
                this._bookList.selectedIndex = -1;
                this._tileItem.petInfo = arg1;
                if (arg1.code != arg1.changeCode) 
                {
                    loc1 = HTMLUtil.addColor(Language.getString(54238), GlobalStyle.color6);
                }
                this._nameTxt.htmlText = loc1 + PetColor.getColorName(arg1);
            }
            else 
            {
                this.clear();
            }
            return;
        }

        public updateStudyBookItem(arg1: ItemData): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            this._studyItem.itemData = arg1;
            if (arg1) 
            {
                loc1 = this._bookList.dataProvider.toArray();
                while (loc3 < loc1.length) 
                {
                    loc2 = loc1[loc3];
                    if (loc2.uid == arg1.uid) 
                    {
                        this._bookList.selectedIndex = loc3;
                    }
                    ++loc3;
                }
            }
            return;
        }

        public reset(): void
        {
            this._forgetBtn.visible = false;
            this.onStudyItemClickHandler(null);
            return;
        }

        /* internal  */updateSkillSprite(arg1: SPetInfo): void
        {
            this._skillListPanel.petInfo = arg1;
            return;
        }

        /* internal  */initListData(): void
        {
            this._bookArray = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isCommomPetSkillBook);
            this._yiArray = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isPetSkillRand);
            this.updateBookList(this._bookArray);
            this.updateYiList(this._yiArray);
            return;
        }

        /* internal  */updateBookList(arg1: Array<any>): void
        {
            var loc2=0;
            var loc1=8;
            this._bookDP.removeAll();
            if (!(arg1 == null) && arg1.length > 0) 
            {
                loc2 = arg1.length;
                if (loc2 <= 8) 
                {
                    loc1 = 8;
                }
                else 
                {
                    loc1 = loc2;
                }
            }
            var loc3=0;
            while (loc3 < loc1) 
            {
                this._bookDP.addItem(arg1[loc3]);
                ++loc3;
            }
            this._bookList.dataProvider = this._bookDP;
            this._bookList.drawNow();
            return;
        }

        /* internal  */updateYiList(arg1: Array<any>): void
        {
            var loc1=null;
            var loc3=0;
            this._yiDP.removeAll();
            var loc2=arg1.length < 8 ? 8 : arg1.length;
            if (arg1 != null) 
            {
                loc3 = 0;
                while (loc3 < loc2) 
                {
                    loc1 = arg1[loc3];
                    if (loc1 && loc1.itemCode == Cache.instance.guidePetSkillRefresh.getGuideItemCode()) 
                    {
                        this._yiDP.addItemAt(loc1, 0);
                    }
                    else 
                    {
                        this._yiDP.addItem(loc1);
                    }
                    ++loc3;
                }
            }
            this._yiList.dataProvider = this._yiDP;
            return;
        }

        /* internal  */onBuyLinkClickHandler(arg1: Object /* flash.events.TextEvent */): void
        {
            QuickBuyWindow.instance.updateWindow(ECategory._ECategoryProp, EProp._EPropPetSkillRand);
            QuickBuyWindow.instance.updateDefaultAmount(1);
            QuickBuyWindow.instance.show();
            return;
        }

        /* internal  */onStudyBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=Cache.instance.role.money;
            if (loc1.coinBind + loc1.coin < this._studyCost) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54150));
                return;
            }
            if (this._studyItem.itemData == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54151));
                return;
            }
            if (SkillConfig.instance.isSpecialSkill(this._tskill.skillId) && this._petInfo.reinLevel <= 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(28028));
                return;
            }
            if (this._petInfo) 
            {
                if (this._tskill) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetLearnSkill, {"uid": this._petInfo.uid, "skillId": this._tskill.skillId}));
                }
            }
            return;
        }

        /* internal  */onPetSkillBookSelectedHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            this._studyItem.itemData = loc1;
            if (loc1) 
            {
                this._studyItem.amount = 1;
            }
            this._tskill = SkillConfig.instance.getInfoByName(loc1.effect);
            if (this._tskill) 
            {
                this._studyCost = this._tskill.needCoin;
                this.updateCost(this._studyCost);
            }
            return;
        }

        /* internal  */updateCost(arg1: int): void
        {
            if (arg1 == 0) 
            {
                this._costTip.visible = false;
                this._costTxt.visible = false;
            }
            else 
            {
                HTMLUtil.addColor(arg1 + Language.getString(54145), GlobalStyle.color1);
                this._costTip.visible = true;
                this._costTxt.visible = true;
            }
            return;
        }

        /* internal  */onStudyItemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._studyItem.itemData) 
            {
                this._studyItem.itemData = null;
                this._bookList.selectedIndex = -1;
            }
            return;
        }

        protected /* override */ onPackChangeHandler(arg1: Object=null): void
        {
            var loc1=null;
            this.initListData();
            if (!(this._bookList.dataProvider.length == 0) && !(this._bookList.selectedIndex == -1)) 
            {
                loc1 = this._bookList.dataProvider.getItemAt(this._bookList.selectedIndex) as ItemData;
            }
            this._studyItem.itemData = loc1;
            return;
        }

        /* internal  */onLinkClickHandler(arg1: Object /* flash.events.TextEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetIlluOpenWindow));
            return;
        }

        /* internal  */onSelectChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc1=this._skillListPanel.selectedSkillInfo;
            if (loc1) 
            {
                this._forgetBtn.visible = true;
            }
            else 
            {
                this._forgetBtn.visible = false;
            }
            return;
        }

        /* internal  */onBtnClickHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc2=null;
            var loc1=(arg1.target as GButton).name;
            if (loc1 != "help") 
            {
                if (loc1 != "illu") 
                {
                    if (loc1 == "forget") 
                    {
                        loc2 = this._skillListPanel.selectedSkillInfo;
                        if (this._petInfo) 
                        {
                            if (loc2) 
                            {
                                Dispatcher.dispatchEvent(new DataEvent(PetEventName.PetOpenForgetSkillWindow, {"petInfo": _petInfo, "skillInfo": loc2}));
                            }
                            else 
                            {
                                MsgManager.showRollTipsMsg(Language.getString(54152));
                            }
                        }
                    }
                }
                else 
                {
                    GameController.petIllu.view.show();
                }
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetSkillHelpWindowOpen));
            }
            return;
        }

        public guideUseItemRefreshSkill(): void
        {
            var loc1=GuideTipsManager.getGuideTips(GuideTipsManager.Dir_TL, Language.getString(21052));
            loc1.x = 390;
            loc1.y = 120;
            addChild(loc1);
            var loc2=this._yiList.getChildAt(0);
            return;
        }

        public clear(): void
        {
            this.onStudyItemClickHandler(null);
            this._skillListPanel.clear();
            this._forgetBtn.visible = false;
            this._nameTxt.text = "";
            this._tileItem.petInfo = null;
            return;
        }

        /* internal */ /* var */_studyCost: int=1000;

        /* internal */ /* var */_rightSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_bookDP: DataProvider;

        /* internal */ /* var */_yiDP: DataProvider;

        /* internal */ /* var */_tileItem: PetTileItem;

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_tskill: TSkill;

        /* internal */ /* var */_leftSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_centerSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_helpBtn: GButton;

        /* internal */ /* var */_illuBtn: GButton;

        /* internal */ /* var */_forgetBtn: GButton;

        /* internal */ /* var */_studyItem: BaseItem;

        /* internal */ /* var */_buyTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_costTip: Object /* flash.text.TextField */;

        /* internal */ /* var */_costTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_studyBtn: GButton;

        /* internal */ /* var */_bookList: TilePanel;

        /* internal */ /* var */_yiList: TilePanel;

        /* internal */ /* var */_petInfo: SPetInfo;

        /* internal */ /* var */_skillSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_skillListPanel: PetSkillListPanel;

        /* internal */ /* var */_bookArray: Array<any>;

        /* internal */ /* var */_yiArray: Array<any>;

        /* internal */ /* var */_skillItemArray: Array<any>;
    }
